package top.aicey.ai.sparring.filter;

import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import top.aicey.ai.sparring.common.constants.TestConstants;
import top.aicey.ai.sparring.common.holder.CurrentUser;
import top.aicey.ai.sparring.common.holder.UserHolder;

/**
 * @ClassName LoginInterceptor
 * @Description TODO
 * @Author yunbo
 * @Date 2021/11/9 11:48
 **/
@Component
public class LocalLoginInterceptor extends HandlerInterceptorAdapter {
    private static final Logger logger = LoggerFactory.getLogger(LocalLoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        /**
         * UserDefinedFastJsonHttpMessageConverter覆盖到去掉这里
         * logger.info("LoginInterceptor#preHandle- ip:{}, uri:{}", XcUtil.getIpAddr(request), request.getRequestURI());
         */
        request.setCharacterEncoding("utf-8");
        if (Objects.equals(request.getMethod(), "OPTIONS")) {
            return true;
        }

        CurrentUser currentUser = new CurrentUser();
        currentUser.setModuleId(TestConstants.MODULE_ID);
        currentUser.setUserId(TestConstants.TEST_USER_ID);
        UserHolder.setUser(currentUser);
        return super.preHandle(request, response, handler);
    }


}
